<?php

namespace catchAdmin\stock\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\stock\model\Slice as SliceModel;
use think\Response;
use think\facade\Db;
use catcher\CatchAuth;
class Slice extends CatchController
{
    
    protected $sliceModel;
    
    /**
     *
     * @time 2022/01/23 13:55
     * @param SliceModel $sliceModel
     * @return mixed
     */
    public function __construct(SliceModel $sliceModel)
    {
        $this->sliceModel = $sliceModel;
    }
    
    /**
     *
     * @time 2022/01/23 13:55
     * @param Request $request
     * @return Response
     */
    public function index(CatchAuth $auth,Request $request) : Response
    {
        $box_id = $request->post('box_id');

        $slice = DB::name('db_slice')->where('box_id',$box_id)->count();
        $user = $auth->user();
        if($slice === 0){
            $this->create($user['tenant_id'],$box_id);
        }
        return CatchResponse::paginate($this->sliceModel->getList());
    }
    
    /**
     *
     * @time 2022/01/23 13:55
     * @return Response
     */
    public function create($tenant_id,$box_id)
    {
        $box = DB::name('db_box')->field('box_id,Good_Qty,Hold_Qty,NG_Qty,config_id,tray_id')->where('box_id',$box_id)->find();

        $Good_Qty = [];
        $Hold_Qty = [];
        $NG_Qty = [];

        if($box['Good_Qty'] > 0){
            for ($i=1; $i<=$box['Good_Qty']; $i++) {
                $Good_Qty[$i]['tray_id'] = $box['tray_id'];
                $Good_Qty[$i]['config_id'] = $box['config_id'];
                $Good_Qty[$i]['box_id'] = $box['box_id'];
                $Good_Qty[$i]['slice_id'] = date('YmdHis').mt_rand(1000,9999);
                $Good_Qty[$i]['number'] = 1;
                $Good_Qty[$i]['goods_status'] = 'Good';
            }
        }
        if($box['Hold_Qty'] > 0){
            for ($i=1; $i<=$box['Hold_Qty']; $i++) {
                $Hold_Qty[$i]['tray_id'] = $box['tray_id'];
                $Hold_Qty[$i]['config_id'] = $box['config_id'];
                $Hold_Qty[$i]['box_id'] = $box['box_id'];
                $Hold_Qty[$i]['slice_id'] = date('YmdHis').mt_rand(1000,9999);
                $Hold_Qty[$i]['number'] = 1;
                $Hold_Qty[$i]['goods_status'] = 'Hold';
            }
        }
        if($box['NG_Qty'] > 0){
            for ($i=1; $i<=$box['NG_Qty']; $i++) {
                $NG_Qty[$i]['tray_id'] = $box['tray_id'];
                $NG_Qty[$i]['config_id'] = $box['config_id'];
                $NG_Qty[$i]['box_id'] = $box['box_id'];
                $NG_Qty[$i]['slice_id'] = date('YmdHis').mt_rand(1000,9999);
                $NG_Qty[$i]['number'] = 1;
                $NG_Qty[$i]['goods_status'] = 'NG';
            }
        }

        $params = array_merge($Good_Qty,$Hold_Qty,$NG_Qty);
        Db::startTrans();
        try {
            $this->sliceModel->insertAllBy($params);
            DB::name('db_box')->where('box_id',$box_id)->update(['split_status'=>1]);
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            return CatchResponse::fail('拆托盘失败');
        }
    }
    
    /**
     *
     * @time 2022/01/23 13:55
     * @param $id
     * @return Response
     */
    public function read($id) : Response
    {
        return CatchResponse::success($this->sliceModel->findBy($id));
    }
    
    /**
     *
     * @time 2022/01/23 13:55
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request) : Response
    {
        $params = $request->post();
        $where['box_id'] = $params['box_id'];
        // $where['id'] = ['<>',$params['id']];
        $where['status'] = 0;
        $slice = DB::name('db_slice')->field('count(goods_status) as goodsstatus,box_id,goods_status')->where($where)->where('id','<>',$params['id'])->group('goods_status')->select();
        
        $Good_Qty = 0;
        $Hold_Qty = 0;
        $NG_Qty = 0;

        foreach ($slice as $key => $value) {
            if($value['goods_status'] === 'Good'){
                $Good_Qty = $value['goodsstatus'];
            }elseif($value['goods_status'] === 'Hold'){
                $Hold_Qty = $value['goodsstatus'];
            }elseif($value['goods_status'] === 'NG'){
                $NG_Qty = $value['goodsstatus'];
            }
        }

        if($params['goods_status'] === 'Good'){
            $Good_Qty = $Good_Qty+1;
        }elseif($params['goods_status'] === 'Hold'){
            $Hold_Qty = $Hold_Qty+1;
        }elseif($params['goods_status'] === 'NG'){
            $NG_Qty = $NG_Qty+1;
        }
       
        Db::startTrans();
        try {
            $this->sliceModel->updateBy($params['id'], $request->post());
            DB::name('db_box')->where('box_id',$params['box_id'])->update([
                        'Good_Qty'  =>  $Good_Qty,
                        'Hold_Qty'  =>  $Hold_Qty,
                        'NG_Qty'    =>  $NG_Qty
                    ]);
            $box = DB::name('db_box')->field('SUM(Good_Qty) as Good_Qty,SUM(Hold_Qty) as Hold_Qty,SUM(NG_Qty) as NG_Qty')->where('tray_id',$params['tray_id'])->find();

            DB::name('db_stock')->where('tray_id',$params['tray_id'])->update([
                'Good_Qty'  =>  $box['Good_Qty'],
                'Hold_Qty'  =>  $box['Hold_Qty'],
                'NG_Qty'    =>  $box['NG_Qty']
                ]);

            Db::commit();
            return CatchResponse::success('修改成功');
        } catch (\Exception $exception) {
            Db::rollback();
            return CatchResponse::fail('修改失败');
        }
    }
    
    /**
     *
     * @time 2022/01/23 13:55
     * @param $id
     * @return Response
     */
    public function delete($id) : Response
    {
        return CatchResponse::success($this->sliceModel->deleteBy($id));
    }
}